using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace labDict.Web.common
{
    public class Utility
    {
        public static void LogErrorAndThrowApplicationException(Exception ex, string message)
        {
            throw new ApplicationException(message, ex);
        }
        /*
        public static 
        */
        public static string CheckString(string value, int maxLength, string emptyMsg, string tooLongMsg)
        {

            if (value != null)
                value = value.Trim();

            if (String.IsNullOrEmpty(value))
                throw new LabTestException(emptyMsg);
            else if (value.Length > maxLength)
                throw new LabTestException(tooLongMsg);

            return value;
        }

        public static string CheckDate(string value, string emptyMsg, string invalidMsg)
        {

            DateTime result;
            bool success = DateTime.TryParse(value, out result);

            if (value != null)
                value = value.Trim();

            if (String.IsNullOrEmpty(value))
                throw new LabTestException(emptyMsg);
            else if (!success)
                throw new LabTestException(invalidMsg);

            return value;
        }

        public static string CheckAndReformatPhone(string phone, string emptyMsg, string tooLongMsg, string invalidFormatMsg)
        {

            if (phone != null)
                phone = phone.Trim().ToLower();

            if (String.IsNullOrEmpty(phone))
            {
                if (String.IsNullOrEmpty(emptyMsg))
                    return "";
                else
                    throw new LabTestException(emptyMsg);
            }

            while (phone.StartsWith("0") || phone.StartsWith("1"))
            {
                phone = phone.Substring(1, phone.Length - 1);
            }

            StringBuilder sbPhone = new StringBuilder();
            for (int i = 0; i < phone.Length; ++i)
            {
                char digit = Convert.ToChar(phone.Substring(i, 1));
                if ('0' <= digit && digit <= '9')
                {
                    switch (sbPhone.Length)
                    {
                        case 3:
                        case 7:
                            sbPhone.Append("-");
                            break;
                        case 12:
                            sbPhone.Append("x");
                            break;
                    }
                    sbPhone.Append(digit);
                }
            }
            phone = sbPhone.ToString();

            if (phone.Length > 20)
                throw new LabTestException(tooLongMsg);
            else if (phone.Length < 12) //123-567-9012
                throw new LabTestException(invalidFormatMsg);

            return phone;

        }

        public static string CheckZip(string zip, string emptyMsg, string invalidFormatMsg)
        {

            if (zip != null)
                zip = zip.Trim();

            if (String.IsNullOrEmpty(zip))
            {
                if (String.IsNullOrEmpty(emptyMsg))
                {
                    return "";
                }
                else
                {
                    throw new LabTestException(emptyMsg);
                }
            }

            if (!Regex.Match(zip, @"^\d{5}(-\d{4})?$").Success)
                throw new LabTestException(invalidFormatMsg);

            return zip;
        }

        public static string CheckPostalCode(string postalCode, string emptyMsg, string invalidFormatMsg)
        {

            if (postalCode != null)
                postalCode = postalCode.Trim().ToUpper();

            if (String.IsNullOrEmpty(postalCode))
            {
                if (String.IsNullOrEmpty(emptyMsg))
                {
                    return "";
                }
                else
                {
                    throw new LabTestException(emptyMsg);
                }
            }

            if (postalCode.Length != 5 && postalCode.Length != 10)
                throw new LabTestException(invalidFormatMsg);

            if (!Regex.Match(postalCode, @"^\d{5}([\-]\d{4})?$").Success)
                throw new LabTestException(invalidFormatMsg);

            return postalCode;
        }


        public static string CheckEmail(string email, int maxLength, string emptyMsg, string invalidFormatMsg)
        {

            if (email != null)
                email = email.Trim();

            if (String.IsNullOrEmpty(email))
            {
                if (String.IsNullOrEmpty(emptyMsg))
                    return "";
                else
                {
                    throw new LabTestException(emptyMsg);
                }
            }
            else
            {
                if (emptyMsg.Length > maxLength)
                    throw new LabTestException(invalidFormatMsg);

                string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                      @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                      @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                Regex re = new Regex(strRegex);
                if (!re.IsMatch(email))
                {
                    throw new LabTestException(invalidFormatMsg);
                }
            }
            return email;
        }

        public static string CheckGender(string gender, string emptyMsg, string invalidFormatMsg)
        {

            if (gender != null)
                gender = gender.Trim().ToUpper();

            if (String.IsNullOrEmpty(gender))
            {
                if (String.IsNullOrEmpty(emptyMsg))
                {
                    return "";
                }
                else
                {
                    throw new LabTestException(emptyMsg);
                }
            }
            else
            {
                if (gender != "M" && gender != "F")
                    throw new LabTestException(invalidFormatMsg);
            }

            return gender;
        }

        public static Decimal CheckNullDecimal(string val)
        {

            if (val != null && val != "")
            {
                return Convert.ToDecimal(val);
            }
            else
            {
                return new Decimal();
            }
        }

        public static void AssignSelectedByValue(ref DropDownList ddl, string val)
        {
            ListItem li = ddl.Items.FindByValue(val);

            if (li != null)
                li.Selected = true;
        }

        public static void SetReadOnly(ControlCollection pControls, bool setreadonly)
        {
            foreach (Control c in pControls)
            {
                if (c is TextBox)
                    ((TextBox)c).Enabled = !setreadonly;
                else if (c is DropDownList)
                    ((DropDownList)c).Enabled = !setreadonly;
                else if (c is CheckBox)
                    ((CheckBox)c).Enabled = !setreadonly;
                else if (c is RadioButton)
                    ((RadioButton)c).Enabled = !setreadonly;
                else if (c is CheckBoxList)
                    ((CheckBoxList)c).Enabled = !setreadonly;
                else if (c is RadioButtonList)
                    ((RadioButtonList)c).Enabled = !setreadonly;
            }
        }

    }
}
